﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;

namespace RabbitMqServerFramework
{
    class Program
    {
        private const string QueueName = "RabbitMQForBreath_doNet";
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            var factory = new ConnectionFactory { HostName = ConfigurationManager.AppSettings["host"], VirtualHost = "dnt_mq", UserName = "lonwin", Password = "123", Port = 5672 };
            var connection = factory.CreateConnection();

            var channel = connection.CreateModel();
            channel.QueueDeclare("RabbitMQForBreath_doNet", true, false, false, null);
            var consumer = new EventingBasicConsumer(channel);
            channel.BasicConsume(QueueName, true, consumer);
            consumer.Received += (model, ea) =>
            {
                var data = JsonConvert.DeserializeObject<LarkMessage>(
                    Encoding.UTF8.GetString(ea.Body.ToArray()));
                if (data != null)
                    Console.WriteLine($"CurrentTime={DateTime.Now:yyyy-MM-dd-hh-mm-ss},Data={data.Data}");
            };
            Console.ReadLine();
        }
    }
}
